<template>
  <div>
    <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="100px" class="login-box" size="medium" label-suffix=":">
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input type="password" v-model="loginForm.pwd" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')">提交</el-button>
        <el-button @click="resetForm('loginForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    var validataUsername = (rules, value, callback) => {
      if (value == "") {
        callback("请输入用户名");
      } else {
        callback();
      }
    };
    var validatePass = (rules, value, callback) => {
      if (value == "") {
        callback("请输入密码");
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        pwd: "",
      },
      rules: {
        username: [{ validator: validataUsername, trigger: "blur" }],
        pwd: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$router.push("/main");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.el-form {
  display: inline-block;
}
</style>
